package ru.tomsk.autovokzal.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.tomsk.autovokzal.R;
import ru.tomsk.autovokzal.activity.adapter.MainMenuAdapter;
import ru.tomsk.autovokzal.tools.MainMenuItem;
import ru.tomsk.autovokzal.tools.networking.command.TimeTableSyncCommand;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.GridView;

public class MainMenuActivity extends Activity {
	/** Called when the activity is first created. */
	List<MainMenuItem> mainMenuArray;
	private MainMenuAdapter adapter;
	private GridView gridView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Map<String, String> params = new HashMap<String, String>();
		params.put(TimeTableSyncCommand.apiUser, "1");
		params.put(TimeTableSyncCommand.sign, "70be128496ac62ffce03df114ba5d898");
		params.put(TimeTableSyncCommand.method, "schedule");
		params.put(TimeTableSyncCommand.signAuth,
				"e7487dcf41548c7036b7ae0810fd416d");

		new TimeTableSyncCommand(params).getJSON(getApplicationContext());
		adapter = new MainMenuAdapter(getApplicationContext(), fillMenuItem());
		gridView = (GridView) findViewById(R.id.gridView1);
		gridView.setAdapter(adapter);

	}

	private List<MainMenuItem> fillMenuItem() {
		List<MainMenuItem> mainMenuArray2 = new ArrayList<MainMenuItem>();
		Intent time_table = new Intent(getApplicationContext(),
				TimetableActivity.class);
		time_table.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mainMenuArray2.add(new MainMenuItem(R.drawable.document,
				getString(R.string.main_menu_item_timetable), time_table,
				getApplicationContext()));

		mainMenuArray2.add(new MainMenuItem(R.drawable.tag,
				getString(R.string.main_menu_item_ticket), null,
				getApplicationContext()));
		mainMenuArray2.add(new MainMenuItem(R.drawable.info,
				getString(R.string.main_menu_item_info), null,
				getApplicationContext()));
		return mainMenuArray2;
	}

	@Override
	protected void onResume() {
		if (adapter != null)
			adapter.notifyDataSetChanged();
		super.onResume();
	}

}